package com.wulis.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.util.ObjectUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Optional;

/**
 * @author wuligao
 * @date 日期：2024年02月04日
 * @description : jackson工具类
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class JacksonUtil {

    /**
     * 日期格式化对象
     */
    private static final DateTimeFormatter DFY_MD_HMS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter DF_HMS = DateTimeFormatter.ofPattern("HH:mm:ss");
    private static final  DateTimeFormatter DF_YMD = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
            .registerModule(new JavaTimeModule());
    private static final ObjectWriter OBJECT_WRITER;

    /**
     * 实体转json
     *
     * @param object 对象
     * @return 返回json
     */
    public static Optional<String> toJsonString(Object object) {
        if (object == null) {
            return Optional.empty();
        } else {
            try {
                return Optional.of(OBJECT_WRITER.writeValueAsString(object));
            } catch (Exception var2) {
                return Optional.empty();
            }
        }
    }

    /**
     * json 转实体
     *
     * @param json  json字符串
     * @param clazz 类
     * @return 返回实体
     */
    public static <T> Optional<T> toObject(String json, Class<T> clazz) {
        if (ObjectUtils.isEmpty(json)) {
            return Optional.empty();
        } else {
            try {
                return Optional.of(OBJECT_MAPPER.readValue(json, clazz));
            } catch (Exception var3) {
                return Optional.empty();
            }
        }
    }

    /**
     * json转实体
     *
     * @param json json字符串
     * @param type 类型
     * @return 返回对象
     */
    public static <T> Optional<T> toObject(String json, TypeReference<T> type) {
        if (ObjectUtils.isEmpty(json)) {
            return Optional.empty();
        } else {
            try {
                return Optional.of(OBJECT_MAPPER.readValue(json, type));
            } catch (JsonProcessingException var3) {
                return Optional.empty();
            }
        }
    }

    static {
        SimpleModule module = new SimpleModule("CustomCarSerializer", new Version(1, 0, 0, null, null, null));
        module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DFY_MD_HMS));
        module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DFY_MD_HMS));
        module.addSerializer(LocalDate.class, new LocalDateSerializer(DF_YMD));
        module.addDeserializer(LocalDate.class, new LocalDateDeserializer(DF_YMD));
        module.addSerializer(LocalTime.class, new LocalTimeSerializer(DF_HMS));
        module.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DF_HMS));
        OBJECT_MAPPER.registerModule(module);
        OBJECT_WRITER = OBJECT_MAPPER.writer();
    }
}
